package com.godme.rabbitmq.listener;

import com.alibaba.fastjson.JSON;
import com.godme.rabbitmq.bean.Order;
import com.godme.rabbitmq.bean.Product;
import com.godme.rabbitmq.props.RabbitProperties;
import com.godme.rabbitmq.repo.OrderRepository;
import com.godme.rabbitmq.repo.ProductRepository;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component

public class RecycleProductListener {

    @Autowired
    ProductRepository productRepository;


    @Autowired
    OrderRepository orderRepository;

    @Autowired
    RabbitProperties rabbitProperties;


    @RabbitListener(queues = "queue.dlx")
    public void testDeadLetterQueueAndThrowsException(Message message){
        try{
            String string = new String(message.getBody());
            Order expireOrder = JSON.parseObject(string, Order.class);

            productRepository.changeResidue(expireOrder.getPid(), 1);

            Order order = orderRepository.findById(expireOrder.getId());
            order.setStatus("已取消");
            orderRepository.update(order);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
